/**
 Brainfuck-Robots Bluetooth App
 Copyright (C) <2014>  <Simon Grafenhorst>

 This file is part of the Brainfuck-Robots Bluetooth App.

 The Brainfuck-Robots Bluetooth App is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 The Brainfuck-Robots Bluetooth App is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with the Brainfuck-Robots Bluetooth App.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.brainfuckrobots.bluetooth.sensors;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.Surface;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import de.brainfuckrobots.bluetooth.ControllingView;
import de.brainfuckrobots.bluetooth.MainActivity;
import de.brainfuckrobots.bluetooth.R;

public class Sensors implements OnSeekBarChangeListener, SensorEventListener {

    private final SensorManager manager;
    private final String tag = "Sensors";
    private final float filter = 0.3f;
    private int sensitivity = 15;
    private float offset[];
    private float orientation[];            //orientation


    public Sensors() {
        offset = new float[3];
        orientation = new float[3];
        manager = (SensorManager) MainActivity.getContext().getSystemService(
                Context.SENSOR_SERVICE);
        registerSeekBarListener();
    }

    private void registerSeekBarListener() {
        ((SeekBar) MainActivity.getContext().findViewById(R.id.sensitivity)).setOnSeekBarChangeListener(this);
        ((SeekBar) MainActivity.getContext().findViewById(R.id.sensitivity)).setMax(100);
        ((SeekBar) MainActivity.getContext().findViewById(R.id.sensitivity)).setProgress(sensitivity - 1);
    }

    public void unregisterOrientationListener() {
        manager.unregisterListener(this);
        ((ControllingView) MainActivity.getContext().findViewById(R.id.controllingView)).resetPad();
    }

    public void registerOrientationListener() {
        manager.registerListener(this, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), manager.SENSOR_DELAY_GAME);
        setOffset();
    }

    public void setOffset() {
        Log.i(tag, "Offset changed to " + offset[0] + " , " + offset[1]);
        offset[0] = orientation[0];
        offset[1] = orientation[1];
        offset[2] = orientation[2];
    }


    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        for (int i = 0; i < orientation.length; i++) {
            orientation[i] = sensorEvent.values[i] * filter + orientation[i] * (1 - filter);
        }
        if(MainActivity.getContext().getWindowManager().getDefaultDisplay().getRotation() == Surface.ROTATION_270){
            ((ControllingView) MainActivity.getContext().findViewById(R.id.controllingView)).
                    setRotationPad((int) ((orientation[1] - offset[1]) * -sensitivity), (int) ((orientation[0] - offset[0]) * -sensitivity));
        } else {
            ((ControllingView) MainActivity.getContext().findViewById(R.id.controllingView)).
                    setRotationPad((int) ((orientation[1] - offset[1]) * sensitivity), (int) ((orientation[0] - offset[0]) * sensitivity));
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
                                  boolean fromUser) {
        sensitivity = (int) (1 + progress);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        //Do nothing
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        //Do nothing
    }
}
